// sharing/pages/frame/frame.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  showLink: false,
	  wifi: '',
	  errMsg: '未打开 Wi-Fi 开关，试试其他WiFi吧~',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this;
	  var wifi = wx.getStorageSync('wifi');
	  var id = options.id;
    wx.getStorage({
      key: 'appcolor',
      success: function (res) {
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: res.data,
        })
        that.setData({ 'appColor': res.data });
      },
    })
	  app.getShareData(function(res){
      that.setData({cfg: res.data});
      if (wifi) {
        wifi.auto = '0';
        that.linkWifi(wifi);
        wx.removeStorageSync('wifi');
      } else {
        wx.startWifi({
          success: function (r) {
            if (id) {
              app.util.request({
                url: 'entry/wxapp/getwifi',
                data: {
                  m: 'junsion_wifinav',
                  t: 1,
                  id: id,
                },
                success: function (r) {
                  if (r.data.code == 1) {
                    var wifi = r.data.wifi;
                    that.linkWifi(wifi);
                  } else {
                    that.setData({ showLink: false, errMsg: r.data.msg });
                  }
                },
              })
            } else {
              wx.getWifiList({
                success: function (res) {
                  if (res.errCode == 0 || res.errMsg == 'getWifiList:ok') {
                    wx.onGetWifiList(function (res) {
                      var bssids = '';
                      var ssids = '';
                      var wifiList = res.wifiList;
                      for (var i in wifiList) {
                        if (bssids) bssids += ',' + wifiList[i].BSSID;
                        else bssids = wifiList[i].BSSID;
                        if (ssids) ssids += ',' + wifiList[i].SSID;
                        else ssids = wifiList[i].SSID;
                      }
                      app.util.request({
                        url: 'entry/wxapp/getwifi',
                        data: {
                          m: 'junsion_wifinav',
                          t: 1,
                          bssids: bssids,
                          ssids: ssids
                        },
                        success: function (r) {
                          if (r.data.code == 1) {
                            var list = r.data.list;
                            for (var n in wifiList) {
                              if (list[wifiList[n].BSSID]) {
                                wifi = list[wifiList[n].BSSID];
                                break;
                              }
                            }
                            if (wifi) {
                              wifi.auto = '1';
                              that.linkWifi(wifi);
                            } else {
                              that.setData({ showLink: false, errMsg: '暂无破解 Wi-Fi，试试其他WiFi吧~' });
                            }
                          }
                        },
                      })

                    });
                  }
                }
              });
            }
          },
          fail: function (r) {
            console.log(r);
          }
        })
      }
    },1);
  },

  linkWifi: function(wifi){
	  var that = this;
	  that.setData({showLink: true});
	  that.setData({wifi: wifi});
	  wx.connectWifi({
		  SSID: wifi.SSID,
		  BSSID: wifi.BSSID,
		  password:wifi.password,
		  success: function(res){
			  if(res.errCode == 0 || res.errMsg== 'connectWifi:ok'){
				  //存在formId
				  if(wifi.formId){
					  app.getUserInfo(function(openid){
					    app.util.request({
			              url: 'entry/wxapp/notice',
			              data: {
			                m: 'junsion_wifinav',
			                t: 1,
			                ssid: wifi.SSID,
			                stitle: wifi.stitle,
			                formId: wifi.formId,
			                openid: openid,
			              }
			            })
					  })
				  }
				  //连接成功创建WiFi
          if (wifi.uncertain == 1) {
            app.util.request({
              url: 'entry/wxapp/uncertain',
              data: {
                m: 'junsion_wifinav',
                t: 1,
                bssid: wifi.BSSID,
                id: wifi.id,
              }
            })
          }else if(wifi.password || wifi.auto == 1){
					  app.util.request({
						  url: 'entry/wxapp/addwifi',
						  data: {
							  m: 'junsion_wifinav',
							  t: 1,
							  bssid: wifi.BSSID,
							  ssid: wifi.SSID,
							  psw: wifi.password
						  }
					  })
				  }
          if(that.data.cfg.saudio){
            app.playAudio(that.data.cfg.saudio,null);
          }
          wx.redirectTo({url: '../status/status'});
			  }else{
				  that.errMsg(res,wifi);
			  }
		  },
		  fail: function(res){
			  console.log(res);
			  that.errMsg(res,wifi);
		  }
	  });
  },
  
  errMsg: function(res,wifi){
    if (this.data.cfg.faudio) {
      app.playAudio(this.data.cfg.faudio, null);
    }

	  var code = res.errCode;
	  var errMsg = res.errMsg;
	  this.setData({showLink: false});
	  if(code == 12001){
		  this.setData({errMsg: '当前系统不支持相关能力'});
	  }else if(code == 12002){
		  //密码错误删除数据库中的WiFi
		  if(wifi.auto){
			  app.util.request({
				  url: 'entry/wxapp/delwifi',
				  data: {
					  m: 'junsion_wifinav',
					  t: 1,
					  bssid: wifi.BSSID
				  },
			  })
		  }
		  this.setData({errMsg: 'Wi-Fi 密码错误，试试其他WiFi吧~'});
	  }else if(code == 12003){
		  this.setData({errMsg: '连接超时，试试其他WiFi吧~'});
	  }else if(code == 12004){
		  this.setData({errMsg: '重复连接 Wi-Fi，试试其他WiFi吧~'});
	  }else if(code == 12007){
		  this.setData({errMsg: '您拒绝授权连接Wi-Fi，试试其他WiFi吧~'});
	  }else if(code == 12008){
		  this.setData({errMsg: '无效Wi-Fi，试试其他WiFi吧~'});
	  }else if(code == 12009){
		  this.setData({errMsg: '系统运营商配置拒绝连接 Wi-Fi，试试其他WiFi吧~'});
	  }else if(code == 12010){
		  this.setData({errMsg: errMsg});
	  }else if(code == 12011){
		  this.setData({errMsg: '应用在后台无法配置 Wi-Fi'});
	  }else{
		  this.setData({errMsg: '系统错误，试试其他WiFi吧~'});
	  }
  },
  
  qitaBtn: function () {
	  wx.switchTab({url: '../index/index'});
  },
 
  onShareAppMessage: function () {
	  return {
	      title: this.data.cfg.indexShare,
	      path: '/sharing/pages/index/index',
	      success: function(res) {
	        // 转发成功
	      },
	      fail: function(res) {
	        // 转发失败
	      }
	    }
  }
})